package com.zjcode.cdcer.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zjcode.cdcer.model.PushDataInfo;
import com.zjcode.cdcer.model.response.ResponseInfoResult;
import com.zjcode.cdcer.model.response.ResponseResult;
import com.zjcode.cdcer.model.response.ResponseUploadResult;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class RestTemplateUtil {



    private static final RestTemplate restTemplate = new RestTemplate();

    private static final HttpHeaders httpHeaders = new HttpHeaders();

    static {
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    }

    public static ResponseEntity<String> restTemplatePost(String requestUrl) {
        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
    }



    public static ResponseEntity<String> restTemplatePost(String requestUrl, ObjectNode objectNode) {
        HttpEntity<String> entity = new HttpEntity<>(objectNode.toString(), httpHeaders);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
    }

    public static ResponseUploadResult restTemplateFilePost(String requestUrl, MultiValueMap<String, Object> formData, HttpHeaders httpHeaders) {
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(formData, httpHeaders);
        return restTemplate.postForObject(requestUrl, requestEntity, ResponseUploadResult.class);
    }

    public static ResponseResult restTemplatePost(String requestUrl, String requestEntity, Map<String, String> headMap) {
        headMap.forEach((k,v) -> {
            httpHeaders.remove(k);
            httpHeaders.add(k, v);
        });
        HttpEntity<String> entity = new HttpEntity<>(requestEntity, httpHeaders);
        return restTemplate.postForObject(requestUrl, entity, ResponseResult.class);
    }

    public static ResponseEntity<String> restSelfPost(String requestUrl, String requestEntity, Map<String, String> headMap) {
        headMap.forEach((k,v) -> {
            httpHeaders.remove(k);
            httpHeaders.add(k, v);
        });
        HttpEntity<String> entity = new HttpEntity<>(requestEntity, httpHeaders);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
    }

    public static ResponseEntity<ResponseInfoResult> restTemplateGet(String requestUrl, MultiValueMap<String,String> params, HttpHeaders httpHeaders) {
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, httpHeaders);
        return restTemplate.exchange(requestUrl, HttpMethod.GET, requestEntity, ResponseInfoResult.class);
    }

    public static void main(String[] args) {
        List<String> str = Arrays.asList("食管未见异常","贲门未见异常");
        System.out.println("=======1:" + str.toString());
        PushDataInfo info = new PushDataInfo();
        info.setNjzd(JSONObject.toJSON(str));
        //info.setNjzd2(JSONObject.parse(str2));
        System.out.println("=======2:" + JSONObject.toJSONString(info));

    }

    public static Long reserveTenDigits(Long number) {
        if (number == null) {
            return null;
        }
        String numberStr = String.valueOf(number);
        if (numberStr.length() <= 10) {
            return number;
        } else {
            return Long.valueOf(numberStr.substring(0, 10));
        }
    }
}
